package com.chengcang.api.modules.busi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chengcang.api.modules.busi.dao.BusiInvoiceHeadDao;
import com.chengcang.api.modules.busi.dto.BusiInvoiceHeadDTO;
import com.chengcang.api.modules.busi.entity.BusiInvoiceHeadEntity;
import com.chengcang.api.modules.busi.service.BusiInvoiceHeadService;
import com.chengcang.api.modules.login.utils.UserHolder;
import com.chengcang.common.constant.Constant;
import com.chengcang.common.exception.CustomException;
import com.chengcang.common.page.PageData;
import com.chengcang.common.service.impl.CrudServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 发票抬头
 *
 * @author zhanli
 * @since 1.0.0 2023-01-09
 */
@Service
public class BusiInvoiceHeadServiceImpl extends CrudServiceImpl<BusiInvoiceHeadDao, BusiInvoiceHeadEntity, BusiInvoiceHeadDTO> implements BusiInvoiceHeadService {

    @Autowired
    BusiInvoiceHeadDao busiInvoiceHeadDao;

    @Override
    public QueryWrapper<BusiInvoiceHeadEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<BusiInvoiceHeadEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        Long tenantId = UserHolder.getUser().getDefaultTenantId();
        wrapper.eq(tenantId != null, "tenant_id", tenantId);
        return wrapper;
    }

    @Override
    public PageData<BusiInvoiceHeadDTO> page(Map<String, Object> params) {

        IPage<BusiInvoiceHeadEntity> ipage = getPage(params, Constant.CREATE_DATE, false);
        List<BusiInvoiceHeadEntity> invoiceHeadEntities = baseDao.selectList(getWrapper(params));
        PageData<BusiInvoiceHeadDTO> page = getPageData(invoiceHeadEntities, ipage.getTotal(), BusiInvoiceHeadDTO.class);

        return page;
    }

    /**
     * 修改原默认发票为非默认
     */
    @Override
    public void updateOldDefault() {
        LambdaUpdateWrapper<BusiInvoiceHeadEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(BusiInvoiceHeadEntity::getDefaulted, 0)
                .eq(BusiInvoiceHeadEntity::getTenantId, UserHolder.getUser().getDefaultTenantId())
                .set(BusiInvoiceHeadEntity::getDefaulted, 1);
        busiInvoiceHeadDao.update(null, lambdaUpdateWrapper);
    }

    /**
     * 逻辑删除发票
     *
     * @param id
     */
    @Override
    public void deleteLogic(Long id) {
        BusiInvoiceHeadDTO busiInvoiceHeadDTO = get(id);
        if (busiInvoiceHeadDTO.getDefaulted() == 0) {
            throw new CustomException("默认发票抬头不允许删除");
        }

        LambdaUpdateWrapper<BusiInvoiceHeadEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(BusiInvoiceHeadEntity::getId, id)
                .eq(BusiInvoiceHeadEntity::getTenantId, UserHolder.getUser().getDefaultTenantId())
                .set(BusiInvoiceHeadEntity::getDeleted, 1);
        busiInvoiceHeadDao.update(null, lambdaUpdateWrapper);
    }
}